package com.juzipi.demo.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.juzipi.demo.mapper.DeptMapper;
import com.juzipi.demo.mapper.StudentMapper;
import com.juzipi.demo.pojo.PageResult;
import com.juzipi.demo.pojo.Student;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class PracticeTkmapperService {


    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private DeptMapper deptMapper;


    public List<Student> queryStuExample(Integer id) {
        //先查询出所有的
        Student student = studentMapper.selectByPrimaryKey(id);
        //再查询出单个的学生，感觉这个操作有点蠢
        List<Student> studentOne = studentMapper.select(student);

        return studentOne;//返回的是 单个学生
    }



    public List<Student> queryAllStu(){
        //查询所有：selectAll，返回一个list集合
        //不需要传参
        List<Student> students = studentMapper.selectAll();
        return students;//返回的是 所有学生
    }



    public Student queryStu(Integer id) {
        //根据主键id查询所有学生
        Student student = studentMapper.selectByPrimaryKey(id);

        return student;// 返回的是 所有学生
    }


    //练习 selectCount 方法
    public List<Student> queryStuListAll(Integer deptId) {
        //懂了，直接传个Student类型的参数就行了，传个id查出所有再传给 selectCount 实在是有点捞了
//        Student student = studentMapper.selectByPrimaryKey(key);
        //不行，还是得传id
        //student 类型的数据没法传吧，只能post了
        //这个不能用GetMapping了
        //应用场景应该是这样的，我再新建一张表来记录学生的对应的系
        //用来查询这个应该可以

//        Student student = studentMapper.selectByPrimaryKey(id);
//        List<Student> students = studentMapper.selectAll();
        List<Student> students = studentMapper.queryStudentAndDept(deptId);

        //需要一个Student类型的参数
        //根据传的参数来计算这个参数对应的数据的数量，例如传个 age 计算等于这个age字段同值的数量
        return students;
    }



    public List<Student> queryStuBlurry(String key) {
        Example example = new Example(Student.class);
        Example.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(key)){
            criteria.andLike("name",key+"%").orEqualTo("age","%"+key+"%").orEqualTo("gender",key+"%");
        }
        criteria.orBetween("age",10,20);
        List<Student> student = studentMapper.selectByExample(example);

        return student;
    }


    public Student queryStuCount(Integer id) {
        Student students = studentMapper.selectByPrimaryKey(id);
        //跟select差不多，区别是这个返回 Student
        Student student = studentMapper.selectOne(students);

        return student;

    }



    public PageResult<Student> queryStuByPart(String key, Integer page, Integer rows) {

//        Example example = new Example(Student.class);
//        Example.Criteria criteria = example.createCriteria();
//        if (StringUtils.isNotBlank(key)){
//            criteria.andLike("name","%"+key+"%").orEqualTo("gender","%"+key+"%").orEqualTo("age","%"+key+"%");
//        }
        PageHelper.startPage(page,rows);

//        List<Student> students = studentMapper.selectByExample(example);
        List<Student> students = studentMapper.queryStudentPart(key);


        PageInfo<Student> pageInfo = new PageInfo<>(students);

        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());

    }



    public List<Student> queryStudentMaxAge(String key) {

        List<Student> students = studentMapper.queryStudentMaxAge(key);
        return students;
    }



    public List<Student> queryStudentDept(String key) {

        List<Student> students = studentMapper.queryStudentDept(key);

        return students;
    }
}
